﻿package mortal.game.view.mail
{
    import Message.Game.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.mail.view.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MailModule extends BaseWindow
    {
        private var _middlePaneBg:ScaleBitmap;
        private var _tabData:Array;
        private var _tabBar:GTabarNew;
        private var _mailList:GTileList;
        private var _mailSelected:ScaleBitmap;
        private var _txtNoMail:GTextFiled;
        private var _pageSelecter:PageSelecter;
        private var _writeOrReadTabBarData:Array;
        private var _writeOrReadTabBar:GTabBar;
        private var _btnDelte:GButton;
        private var _btnGetAttachment:TimeButton;
        private var _allCheckBox:GCheckBox;
        private var _checkBox1:GCheckBox;
        public var mailReadPanel:MailReadPanel;
        public var mailWritePanel:MailWritePanel;
        private var _txtGMTip:GTextFiled;
        public var uiLoadComplete:Boolean = false;
        private var _preSelectIndex:int;
        private var _openWritePanel:Boolean = false;
        private var _readOrWrite:int = 0;
        private var _toPlayerName:String = "";
        private var delUnreadMailArr:Array;

        public function MailModule(param1:ILayer = null)
        {
            super(param1);
            this.init();
            return;
        }// end function

        public function init() : void
        {
            this._tabData = Language.getArray(50002);
            this._writeOrReadTabBarData = [{label:"收件箱", name:"inbox"}];
            setSize(635, 420);
            title = Language.getString(50001);
            titleHeight = 60;
            this.isHideDispose = true;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.mail, this.showSkin);
            return;
        }// end function

        private function showSkin() : void
        {
            var _loc_1:* = Cache.instance.mail;
            this._middlePaneBg = UIFactory.bg(15, 67, 298, 311, this);
            this._tabBar = UIFactory.gTabBarNew(3, 32, this._tabData, 631, 385, 65, 26, this, this.tabBarChangeHandler);
            this._tabBar.selectedIndex = 0;
            this._txtNoMail = UIFactory.gTextField("暂无邮件", 135, 200, 60, 25, this);
            this._txtNoMail.textColor = GlobalStyle.colorHongUint;
            this._mailList = UIFactory.tileList(21, 75, 289, 269, this);
            this._mailList.rowHeight = 54;
            this._mailList.columnWidth = 287;
            this._mailList.rowCount = 5;
            this._mailList.horizontalGap = 0;
            this._mailList.verticalGap = 0;
            this._mailList.setStyle("cellRenderer", MailListItem);
            addChild(this._mailList);
            this._mailSelected = UIFactory.bg(-3, -3, 291, 54, this._mailList, ImagesConst.selectedBg);
            this._mailSelected.visible = false;
            this._pageSelecter = UIFactory.pageSelecter(126, 350, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._btnDelte = UIFactory.gButton("删除", 26, 350, 50, 22, this);
            this._btnGetAttachment = UIFactory.timeButton("全提", 257, 350, 50, 22, this);
            this._btnGetAttachment.cdTime = 10000;
            this._allCheckBox = UIFactory.checkBox("全部", 28, 383, 50, 28, this);
            this._checkBox1 = UIFactory.checkBox("提取后删除", 260, 383, 100, 28, this);
            if (_loc_1)
            {
                this._checkBox1.selected = _loc_1.mailGetAfterDelete;
            }
            this._txtGMTip = UIFactory.textField("提示：切勿相信索取账号密码/公会/免费等相关的信息", 345, 387, 350, 20, this);
            this._txtGMTip.textColor = GlobalStyle.colorHongUint;
            this._writeOrReadTabBar = UIFactory.gTabBar(this._middlePaneBg.x + this._middlePaneBg.width + 6, (this._middlePaneBg.y - 1), this._writeOrReadTabBarData, 57, 24, this, this.onWriteOrReadChange, "ButtonNew");
            this._mailList.configEventListener(Event.CHANGE, this.lisetSecletHandler);
            this._allCheckBox.configEventListener(MouseEvent.CLICK, this.btnAllClickHandler);
            this._checkBox1.configEventListener(MouseEvent.CLICK, this.checkBox1ClickHandler);
            this._btnDelte.configEventListener(MouseEvent.CLICK, this.btnDeleteClickHandler);
            this._btnGetAttachment.configEventListener(MouseEvent.CLICK, this.btnGetAttachHandler);
            if (this._openWritePanel)
            {
                this._writeOrReadTabBar.selectedIndex = 1;
            }
            else
            {
                this._writeOrReadTabBar.selectedIndex = 0;
            }
            this._preSelectIndex = 0;
            this.onWriteOrReadChange();
            this._mailList.selectable = true;
            if (!this.uiLoadComplete)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailQueryAll));
                this.uiLoadComplete = true;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this.uiLoadComplete == false)
            {
                return;
            }
            this.uiLoadComplete = false;
            this._openWritePanel = false;
            this._readOrWrite = 0;
            this._toPlayerName = "";
            this._middlePaneBg.dispose(param1);
            this._tabBar.dispose(param1);
            this._txtNoMail.dispose(param1);
            this._mailSelected.dispose(param1);
            this._mailList.dispose(param1);
            this._pageSelecter.dispose(param1);
            this._btnDelte.dispose(param1);
            this._btnGetAttachment.dispose(param1);
            this._allCheckBox.dispose(param1);
            this._checkBox1.dispose(param1);
            this._txtGMTip.dispose(param1);
            this._writeOrReadTabBar.dispose(param1);
            if (this.mailReadPanel)
            {
                var _loc_2:Boolean = true;
                this.mailReadPanel.mouseChildren = true;
                var _loc_2:* = _loc_2;
                this.mailReadPanel.mouseEnabled = _loc_2;
                this.mailReadPanel.visible = _loc_2;
                this.mailReadPanel.dispose(param1);
            }
            if (this.mailWritePanel)
            {
                var _loc_2:Boolean = true;
                this.mailWritePanel.mouseChildren = true;
                var _loc_2:* = _loc_2;
                this.mailWritePanel.mouseEnabled = _loc_2;
                this.mailWritePanel.visible = _loc_2;
                this.mailWritePanel.dispose(param1);
            }
            this._middlePaneBg = null;
            this._tabBar = null;
            this._txtNoMail = null;
            this._mailSelected = null;
            this._mailList = null;
            this._pageSelecter = null;
            this._btnDelte = null;
            this._btnGetAttachment = null;
            this._allCheckBox = null;
            this._checkBox1 = null;
            this._txtGMTip = null;
            this._writeOrReadTabBar = null;
            this.mailReadPanel = null;
            this.mailWritePanel = null;
            return;
        }// end function

        private function lisetSecletHandler(event:Event) : void
        {
            if (this._mailList.selectedIndex < 0 || this._mailList.selectedIndex > (this._mailList.rowCount - 1))
            {
                this._mailSelected.visible = false;
                return;
            }
            var _loc_2:* = this._mailList.itemToCellRenderer(this._mailList.selectedItem) as MailListItem;
            this._mailSelected.x = _loc_2.x;
            this._mailSelected.y = _loc_2.y;
            this._mailSelected.visible = true;
            this._mailList.addChild(this._mailSelected);
            this.openOneMail(this._mailList.selectedIndex);
            this._writeOrReadTabBar.selectedIndex = 0;
            this.onWriteOrReadChange(null);
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._pageSelecter.currentPage = 1;
            this.queryMail();
            return;
        }// end function

        public function queryMail() : void
        {
            var _loc_1:* = Cache.instance.mail;
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * this._mailList.rowCount;
            if (_loc_2 > (_loc_1.mailTotalCount - 1))
            {
                _loc_2 = _loc_2 - this._mailList.rowCount < 0 ? (0) : (_loc_2 - this._mailList.rowCount);
            }
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailQueryAll, _loc_2));
                    break;
                }
                case 1:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailQueryUnread, _loc_2));
                    break;
                }
                case 2:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailQueryRead, _loc_2));
                    break;
                }
                case 3:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailQuerySys, _loc_2));
                    break;
                }
                case 4:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailQueryPers, _loc_2));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.clearMailSelect();
            var _loc_2:Boolean = false;
            this._pageSelecter.mouseChildren = false;
            this._pageSelecter.mouseEnabled = _loc_2;
            this.queryMail();
            return;
        }// end function

        public function updateMailList(param1:Boolean = false) : void
        {
            var _loc_2:int = 0;
            var _loc_3:SMail = null;
            var _loc_4:Boolean = true;
            this._pageSelecter.mouseChildren = true;
            this._pageSelecter.mouseEnabled = _loc_4;
            if (this._mailList == null)
            {
                return;
            }
            this._mailList.dataProvider = this.getDataProvider1();
            this._mailList.drawNow();
            this._mailList.selectedIndex = -1;
            if (this.mailReadPanel && this.mailReadPanel.data)
            {
                _loc_2 = 0;
                while (_loc_2 < this._mailList.dataProvider.length)
                {
                    
                    _loc_3 = this._mailList.getItemAt(_loc_2) as SMail;
                    if (_loc_3.mailId == this.mailReadPanel.data.mailId)
                    {
                        this._mailList.selectedIndex = _loc_2;
                    }
                    _loc_2++;
                }
            }
            this.lisetSecletHandler(null);
            this.updatePageSeleter();
            this._txtNoMail.visible = Boolean(this._mailList.dataProvider.length == 0);
            if (param1)
            {
                this.openOneMail(this._mailList.selectedIndex);
            }
            return;
        }// end function

        public function updatePageSeleter() : void
        {
            var _loc_1:* = Cache.instance.mail;
            this._pageSelecter.maxPage = Math.ceil(_loc_1.mailTotalCount / this._mailList.rowCount);
            this._pageSelecter.currentPage = Math.ceil((_loc_1.mailStartIndex + 1) / this._mailList.rowCount);
            return;
        }// end function

        public function openOneMail(param1:int = 0) : void
        {
            if (this._mailList.selectedIndex == -1)
            {
                this.mailReadPanel.showMail(null);
                return;
            }
            if (this._mailList.dataProvider.length > 0)
            {
                this.mailReadPanel.showMail(this._mailList.dataProvider.getItemAt(param1) as SMail);
            }
            else
            {
                this.mailReadPanel.showMail(null);
            }
            return;
        }// end function

        public function clearMailSelect() : void
        {
            var _loc_2:MailListItem = null;
            var _loc_1:int = 0;
            while (_loc_1 < this._mailList.length)
            {
                
                _loc_2 = this._mailList.itemToCellRenderer(this._mailList.getItemAt(_loc_1)) as MailListItem;
                if (_loc_2)
                {
                    _loc_2.mailSelected = false;
                }
                _loc_1++;
            }
            this._allCheckBox.selected = false;
            return;
        }// end function

        private function onWriteOrReadChange(event:MuiEvent = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this._writeOrReadTabBar.selectedIndex == 0)
            {
                this.readOrWrite = 1;
            }
            else if (this._writeOrReadTabBar.selectedIndex == 1)
            {
                _loc_2 = Cache.instance.role.entityInfo.level;
                _loc_3 = GameDefConfig.instance.getSystemOpenLevel(87);
                if (_loc_2 >= _loc_3)
                {
                    this.readOrWrite = 2;
                }
                else
                {
                    if (this._writeOrReadTabBar.selectedIndex != this._preSelectIndex)
                    {
                        this._writeOrReadTabBar.selectedIndex = this._preSelectIndex;
                    }
                    MsgManager.showRollTipsMsg(String(_loc_3) + "级后开放写邮件功能");
                }
            }
            this._preSelectIndex = this._writeOrReadTabBar.selectedIndex;
            return;
        }// end function

        public function set readOrWrite(param1:int) : void
        {
            if (this._readOrWrite == param1)
            {
                return;
            }
            this._readOrWrite = param1;
            if (this._readOrWrite == 1)
            {
                if (this.mailWritePanel)
                {
                    var _loc_2:Boolean = false;
                    this.mailWritePanel.mouseChildren = false;
                    var _loc_2:* = _loc_2;
                    this.mailWritePanel.mouseEnabled = _loc_2;
                    this.mailWritePanel.visible = _loc_2;
                }
                if (this.mailReadPanel == null)
                {
                    this.mailReadPanel = UICompomentPool.getUICompoment(MailReadPanel);
                    this.mailReadPanel.createDisposedChildren();
                    this.mailReadPanel.x = this._middlePaneBg.x + this._middlePaneBg.width + 2;
                    this.mailReadPanel.y = this._writeOrReadTabBar.y + 22;
                    this.addChild(this.mailReadPanel);
                    this.mailReadPanel.btnReply.configEventListener(MouseEvent.CLICK, this.bntReplyClickHandler, false, 0, true);
                }
                var _loc_2:Boolean = true;
                this.mailReadPanel.mouseChildren = true;
                var _loc_2:* = _loc_2;
                this.mailReadPanel.mouseEnabled = _loc_2;
                this.mailReadPanel.visible = _loc_2;
            }
            else if (this._readOrWrite == 2)
            {
                if (this.mailReadPanel)
                {
                    var _loc_2:Boolean = false;
                    this.mailReadPanel.mouseChildren = false;
                    var _loc_2:* = _loc_2;
                    this.mailReadPanel.mouseEnabled = _loc_2;
                    this.mailReadPanel.visible = _loc_2;
                }
                if (this.mailWritePanel == null)
                {
                    this.mailWritePanel = UICompomentPool.getUICompoment(MailWritePanel);
                    this.mailWritePanel.createDisposedChildren();
                    this.mailWritePanel.x = this._middlePaneBg.x + this._middlePaneBg.width + 2;
                    this.mailWritePanel.y = this._writeOrReadTabBar.y + 22;
                    this.addChild(this.mailWritePanel);
                }
                var _loc_2:Boolean = true;
                this.mailWritePanel.mouseChildren = true;
                var _loc_2:* = _loc_2;
                this.mailWritePanel.mouseEnabled = _loc_2;
                this.mailWritePanel.visible = _loc_2;
                this.mailWritePanel.setToPlyerName(this._toPlayerName);
            }
            return;
        }// end function

        private function bntReplyClickHandler(event:MouseEvent) : void
        {
            this._toPlayerName = this.mailReadPanel.data.fromPlayerName;
            this._writeOrReadTabBar.selectedIndex = 1;
            this.onWriteOrReadChange(null);
            return;
        }// end function

        private function getDataProvider1() : DataProvider
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_1:* = Cache.instance.mail;
            var _loc_2:* = new DataProvider();
            var _loc_3:* = _loc_1.mailVec;
            var _loc_4:* = this._mailList.rowCount;
            if (_loc_3.length)
            {
                _loc_5 = Math.min(_loc_4, _loc_3.length);
                _loc_6 = 0;
                while (_loc_6 < _loc_5)
                {
                    
                    _loc_2.addItem(_loc_3[_loc_6]);
                    _loc_6++;
                }
            }
            return _loc_2;
        }// end function

        private function checkBox1ClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.mail;
            _loc_2.mailGetAfterDelete = this._checkBox1.selected;
            return;
        }// end function

        private function btnAllClickHandler(event:MouseEvent) : void
        {
            var _loc_3:MailListItem = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._mailList.length)
            {
                
                _loc_3 = this._mailList.itemToCellRenderer(this._mailList.getItemAt(_loc_2)) as MailListItem;
                _loc_3.mailSelected = this._allCheckBox.selected;
                _loc_2++;
            }
            return;
        }// end function

        private function btnDeleteClickHandler(event:MouseEvent) : void
        {
            var _mailCache:MailCache;
            var boo:Boolean;
            var arr:Array;
            var data:SMail;
            var m:MailListItem;
            var e:* = event;
            var onSelect:* = function (param1:int) : void
            {
                var _loc_2:int = 0;
                var _loc_3:Object = null;
                _loc_2 = arr.length - 1;
                while (_loc_2 > -1)
                {
                    
                    if (_mailCache.checkMailhadAttach(_mailCache.getMailByMailId(arr[_loc_2])))
                    {
                        MsgManager.showRollTipsMsg("不能删除带附件的邮件");
                        return;
                    }
                    _loc_2 = _loc_2 - 1;
                }
                _loc_2 = arr.length - 1;
                while (_loc_2 > -1)
                {
                    
                    if (_mailCache.checkMailhadUnreak(_mailCache.getMailByMailId(arr[_loc_2])))
                    {
                        delUnreadMailArr = arr;
                        Alert.show("选中邮件含未读邮件，是否确定删除？", null, Alert.OK | Alert.CANCEL, null, onDelUnreadMailHd);
                        return;
                    }
                    _loc_2 = _loc_2 - 1;
                }
                if (arr.length == 0)
                {
                    MsgManager.showRollTipsMsg("没有可删除的邮件");
                    return;
                }
                if (param1 == Alert.OK)
                {
                    _loc_3 = {};
                    _loc_3.boo = boo;
                    _loc_3.arr = arr;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MailDelete, _loc_3));
                }
                return;
            }// end function
            ;
            _mailCache = Cache.instance.mail;
            boo;
            arr;
            var i:int;
            while (i < this._mailList.length)
            {
                
                data = this._mailList.getItemAt(i) as SMail;
                m = this._mailList.itemToCellRenderer(data) as MailListItem;
                if (m.mailSelected)
                {
                    arr.push(data.mailId);
                }
                i = (i + 1);
            }
            if (arr.length == 0)
            {
                MsgManager.showRollTipsMsg("请选择邮件");
                return;
            }
            this.onSelect(Alert.OK);
            return;
        }// end function

        private function onDelUnreadMailHd(param1:int) : void
        {
            var _loc_2:Object = null;
            if (param1 == Alert.OK)
            {
                _loc_2 = {};
                _loc_2.boo = false;
                _loc_2.arr = this.delUnreadMailArr;
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailDelete, _loc_2));
            }
            return;
        }// end function

        private function btnGetAttachHandler(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.mail;
            var _loc_3:Array = [];
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.mailVec.length)
            {
                
                _loc_3.push(_loc_2.mailVec[_loc_4].mailId);
                _loc_4++;
            }
            if (_loc_3.length == 0)
            {
                MsgManager.showRollTipsMsg("暂无邮件");
                return;
            }
            if (Cache.instance.pack.backPackCache.isPackFull())
            {
                return MsgManager.showRollTipsMsg("背包已满", MsgRollTipsType.msgRollTips3);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.MailGetAttachments, _loc_3));
            return;
        }// end function

        public function openWritePanelFunc(param1:String) : void
        {
            this._toPlayerName = param1;
            this._openWritePanel = true;
            return;
        }// end function

    }
}
